package com.canxin.service.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.canxin.entity.UuidEntity;
import com.canxin.service.WechatRobotServiceI;

import weixin.guanjia.core.util.WeixinUtil;

@Service("wechatRobotService")
@Transactional
public class WechatRobotImpl extends CommonServiceImpl implements  WechatRobotServiceI{

	@Override
	public String getUUID() throws UnsupportedEncodingException {
		StringBuffer url = new StringBuffer("https://login.wx.qq.com/jslogin?");
		url.append("&appid=wx782c26e4c19acffb");
		url.append("&redirect_uri="+URLEncoder.encode("https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage", "UTF-8"));
		url.append("&fun=new");
		url.append("&lang=zh_CN");
		url.append("&_="+String.valueOf(System.currentTimeMillis()));
		System.setProperty("jsse.enableSNIExtension", "false");
		String result  = WeixinUtil.httpRequestForString(url.toString(), "GET", "");
		org.jeecgframework.core.util.LogUtil.info(result);
		String uuid = result.substring(result.lastIndexOf("uuid = "));
		uuid = uuid.replace("uuid = \"", "");
		uuid = uuid.replace("\";", "");
		org.jeecgframework.core.util.LogUtil.info(uuid);
		saveUUID(uuid);
		return uuid;
	}

	@Override
	public void saveUUID(String uuid) {
		// TODO Auto-generated method stub
		UuidEntity uuidEntity = new UuidEntity();
		uuidEntity.setUuid(uuid);
		uuidEntity.setCreateDate(new Date());
		uuidEntity.setStatus("0");
		super.save(uuidEntity);
	}
	
	
	
}